<template>
  <div class="container">
    <!-- 工具条 -->
    <el-form :inline="true" :data="noticeData" :model="filters" class="demo-form-inline" style="margin-top:40px;margin-left:1%">
      <el-form-item label="通知列表">
        <el-input placeholder="请输入你要查询的通知"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="内容">
              <span>{{ props.row.content }}</span>
            </el-form-item>
            <el-form-item label="创建时间">
              <span>{{ props.row.createTime | formatDate }}</span>
            </el-form-item>
            <el-form-item label="置顶">
              <el-switch
                v-model="props.row.topping"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="changeTopping"/>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title"/>
      <el-table-column label="创建的时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | formatDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="置顶">
        <template slot-scope="scope">
          <span>
            <el-switch
              v-model="scope.row.topping"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeTopping($event,scope.row)"/>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--新增界面-->
    <el-dialog :visible.sync="addFormVisible" :close-on-click-modal="false" title="新增" style="z-index:9999">
      <el-form ref="addForm" :rules="addFormRules" :model="addForm" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="addForm.title" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="通知内容" prop="content">
          <el-input v-model="addForm.content" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="通知置顶" prop="topping">
          <el-switch
            v-model="addForm.topping"
            active-color="#13ce66"
            inactive-color="#ff4949"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button :loading="addLoading" type="primary" @click.native="addSubmit">提交</el-button>
      </div>
    </el-dialog>
    <!-- 更新界面 -->
    <el-dialog :visible.sync="editFormVisible" :close-on-click-modal="false" title="编辑">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="editForm.title" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="editForm.content" auto-complete="off"/>
        </el-form-item>
        <el-form-item label="通知置顶" prop="topping">
          <el-switch
            v-model="editForm.topping"
            active-color="#13ce66"
            inactive-color="#ff4949"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button :loading="editLoading" type="primary" @click.native="editSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { mapGetters } from 'vuex'
import { getNotice, addNotice, updateNotice, removeNotice } from '../../api/notice'
import { formatDate } from '@/common/js/data.js'
export default {
  filters: {
    formatDate(time) {
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  },
  data() {
    return {
      tableData: [],
      noticeData: [],
      filters: {},
      // 添加界面是否显示
      addFormVisible: false,
      editFormVisible: false,
      addLoading: false,
      editLoading: false,
      addForm: {
        title: '',
        content: '',
        topping: false
      },
      editForm: {
        title: '',
        content: '',
        topping: false
      },
      addFormRules: {
        title: [{ required: true, message: '请输入通知标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入通知内容', trigger: 'blur' }]
      },
      editFormRules: {
        title: [{ required: true, message: '请输入通知标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入通知内容', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getMyNotice()
  },
  methods: {
    // 显示新增界面
    handleAdd: function() {
      this.addFormVisible = true
      this.addForm = {
        title: '',
        content: '',
        topping: false
      }
    },
    // 显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      this.editForm = Object.assign({}, row)
    },
    handleDel: function(index, row) {
      removeNotice(row).then(() => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getMyNotice()
      })
    },
    getMyNotice() {
      getNotice().then(res => { console.log(this.tableData = res.data) })
    },
    addMyNotice(data) {
      addNotice(data)
    },
    changeTopping(topping, data) {
      console.log(topping)
      updateNotice(data).then((res) => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getMyNotice()
      })
    },
    // 保存新增
    addSubmit: function() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true
            const para = Object.assign({}, this.addForm)
            addNotice(para).then((res) => {
              this.addLoading = false
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$refs['addForm'].resetFields()
              this.addFormVisible = false
              // this.reload()
              this.getMyNotice()
            })
          })
        }
      })
    },
    // 更新提交
    editSubmit: function() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true
            const para = Object.assign({}, this.editForm)
            updateNotice(para).then((res) => {
              this.addLoading = false
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.$refs['editForm'].resetFields()
              this.editFormVisible = false
              // this.reload()
              this.getMyNotice()
            })
          })
        }
      })
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
